package com.bgs.timing;


import com.bgs.entity.Commodityimg;
import com.bgs.entity.Commodityimgunion;
import com.bgs.entity.Selection;
import com.bgs.pojo.CommodityMax;
import com.bgs.pojo.CommodityUnion;
import com.bgs.service.LjpexclusiveService;
import com.bgs.tool.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class Test {

    @Autowired
    LjpexclusiveService ljpexclusiveService;

    @Autowired
    RedisUtil redisUtil;

    //3.添加定时任务
    //@Scheduled(cron = "0/5 * * * * ?")
    //或直接指定时间间隔，例如：5秒
    @Scheduled(fixedRate=86400000)//86400000
    private void configureTasks() {
        System.out.println("ssssssss");
//        List<CommodityMax> list=ljpexclusiveService.getcommodityquan();
////        List<Commodityimg> img=ljpexclusiveService.getcommodityimg();
////        List<Commodityimgunion> union=ljpexclusiveService.getcommodityunion();
//        List<Selection> selection=ljpexclusiveService.getselection();
//
//        for (CommodityMax commodityMax : list) {
//            //Commodityimgunion commodityimgunion
//            for (Commodityimgunion commodityimgunion : commodityMax.getCommodityimgunions()) {
////                System.out.println(commodityimgunion);
////                System.out.println(commodityimg);
////                if(commodityimgunion.getCiId()==commodityimg.getCiId()){
////
////                }
//                List<Commodityimg> img=ljpexclusiveService.getcommodityimg(commodityimgunion.getCiId());
//                commodityMax.setCommodityimgs(img);
//            }
//        }
////        for (CommodityMax commodityMax : list) {
////            for (Selection selection1 : selection) {
////                if(selection1.getCommodityId()==commodityMax.getCommodityId()){
////                    commodityMax.getSelections().add(selection1);
////                }
////            }
////        }
//        for (CommodityMax commodityMax : list) {
////            for (Commodityimgunion commodityimgunion : union) {
////                if(commodityimgunion.getCommodityId()==commodityMax.getCommodityId()){
////                    commodityMax.getCommodityimgunions().add(commodityimgunion);
////                }
////            }
//            List<Commodityimgunion> union=ljpexclusiveService.getcommodityunion(commodityMax.getCommodityId());
//            commodityMax.setCommodityimgunions(union);
//
//        }
        List<CommodityMax> maxList=ljpexclusiveService.commoditymaxquan();
        for (CommodityMax commodityMax : maxList) {
            commodityMax.setSum(ljpexclusiveService.sunck(commodityMax.getCommodityId()));
        }

        for (CommodityMax commodityMax : maxList) {
            commodityMax.setNewprice(commodityMax.getPrice()+50);
        }


        System.out.println("maxList:"+maxList);
        List<CommodityMax> list1=new ArrayList<>();
        list1 = maxList.stream().sorted(Comparator.comparing(CommodityMax::getSum)).collect(Collectors.toList());



        LocalDateTime localDateTime=LocalDateTime.now();
        int year = localDateTime.getYear();
        int monthValue = localDateTime.getMonthValue();
        int dayOfMonth = localDateTime.getDayOfMonth();
        String times=year+"-"+monthValue+"-"+dayOfMonth;
        if(redisUtil.get("redeem")==null){
            Map<String,List<CommodityMax>> map=new HashMap();
            map.put(times,list1);
            redisUtil.set("redeem",map,86400);
        }else {
            Map<String,List<CommodityMax>> map=(Map<String,List<CommodityMax>>)redisUtil.get("redeem");
            map.put(times,list1);
            redisUtil.set("redeem",map,86400);
        }







    }




}
